clear
clear matrix

set mem 150m

clear
insheet using "$sourcedatadir/us/beagdpdeflator.csv", names
save "$gendatadir/us_beagdpdeflator.dta", replace

clear
insheet using "$sourcedatadir/us/fred_pce_deflator.csv", names
gen date = date(observation_date, "YMD")
format date %td
gen year = year(date)
rename dpcerd3a086nbea pcedeflator
keep year pcedeflator
save "$gendatadir/us_fred_pce_deflator.dta", replace

clear
insheet using "$sourcedatadir/us/bea_table21.csv"
gen inc_pti_mean = (1*10^9 * personalincome) / (populationmidperiodthousands * 1*10^3)
keep year inc_pti_mean personalincome
save "$gendatadir/us_bea_table21.dta", replace

clear
insheet using "$sourcedatadir/us/f01AR_cleaned.csv"
sort year
save "$gendatadir/us_f01AR_cleaned.dta", replace

clear
insheet using "$sourcedatadir/us/f02AR_cleaned.csv"
sort year
save "$gendatadir/us_f02AR_cleaned.dta", replace

clear
insheet using "$sourcedatadir/us/f03AR_cleaned.csv"
sort year
save "$gendatadir/us_f03AR_cleaned.dta", replace

clear
insheet using "$sourcedatadir/us/uselections.csv", names

merge 1:1 year using "$gendatadir/us_f01AR_cleaned.dta", nogen
merge 1:1 year using "$gendatadir/us_f02AR_cleaned.dta", nogen
merge 1:1 year using "$gendatadir/us_f03AR_cleaned.dta", nogen
merge 1:1 year using "$gendatadir/us_bea_table21.dta", nogen

tsset year

gen inc_fti_mean = (1*10^9 * personalincome) / (numberfamilies * 1*10^3)

merge 1:1 country year using "$gendatadir/wtid_processed.dta", keep(master match)
drop _merge

merge 1:1 year using "$gendatadir/us_beagdpdeflator.dta", nogen
merge 1:1 year using "$gendatadir/us_fred_pce_deflator.dta", nogen


//// Deflate inc_mean
// We can choose between gdpdeflator and pcedeflator
gen deflator = pcedeflator
rename inc_pti_mean inc_curr_pti_mean
gen    inc_pti_mean = 100 * inc_curr_pti_mean / deflator
rename inc_fti_mean inc_curr_fti_mean
gen    inc_fti_mean = 100 * inc_curr_fti_mean / deflator

tsset year
sort year

// Generate growth rates for the various income groups
foreach inctype in m ul {
  gen inc_ti_quin1_`inctype'_g1   = (inc_ti_quin1_`inctype' - l.inc_ti_quin1_`inctype')/l.inc_ti_quin1_`inctype'
  gen inc_ti_quin2_`inctype'_g1   = (inc_ti_quin2_`inctype' - l.inc_ti_quin2_`inctype')/l.inc_ti_quin2_`inctype'
  gen inc_ti_quin3_`inctype'_g1   = (inc_ti_quin3_`inctype' - l.inc_ti_quin3_`inctype')/l.inc_ti_quin3_`inctype'
  gen inc_ti_quin4_`inctype'_g1   = (inc_ti_quin4_`inctype' - l.inc_ti_quin4_`inctype')/l.inc_ti_quin4_`inctype'
}
gen inc_ti_quin12_m     = (inc_ti_quin1_m + inc_ti_quin2_m) / 2
gen inc_ti_quin12_m_g1  = (inc_ti_quin12_m - l.inc_ti_quin12_m) / l.inc_ti_quin12_m
gen inc_ti_quin23_ul    = (inc_ti_quin2_ul + inc_ti_quin3_ul) / 2
gen inc_ti_quin23_ul_g1 = (inc_ti_quin23_ul - l.inc_ti_quin23_ul) / l.inc_ti_quin23_ul
gen inc_ti_top5_m_g1    = (inc_ti_top5_m  - l.inc_ti_top5_m) /l.inc_ti_top5_m
gen inc_ti_top5_ll_g1   = (inc_ti_top5_ll - l.inc_ti_top5_ll)/l.inc_ti_top5_ll

*gen inc_disp_g1      = (income_disp_c2005 - l.income_disp_c2005)/l.income_disp_c2005
gen inc_pti_mean_g1  = (inc_pti_mean - l.inc_pti_mean)/l.inc_pti_mean
gen inc_fti_mean_g1  = (inc_fti_mean - l.inc_fti_mean)/l.inc_fti_mean
gen inc_ti_mean      = inc_ti_quin1_m + inc_ti_quin2_m + inc_ti_quin3_m + inc_ti_quin4_m + inc_ti_quin5_m
gen inc_ti_mean_g1   = (inc_ti_mean - l.inc_ti_mean) / l.inc_ti_mean

